package cn.itrigger.dynamic.sql.select;

import cn.itrigger.dynamic.sql.render.GuaranteedTableAliasCalculator;
import cn.itrigger.dynamic.sql.render.TableAliasCalculator;
import cn.itrigger.dynamic.sql.select.join.JoinModel;
import cn.itrigger.dynamic.sql.structure.BasicColumn;
import cn.itrigger.dynamic.sql.structure.SqlTable;
import cn.itrigger.dynamic.sql.where.WhereModel;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;

import static cn.itrigger.dynamic.sql.util.StringUtilities.spaceBefore;

/**
 * @author admin
 * @date 2018/9/25
 **/
public class QueryExpressionModel {

    private String connector;
    private boolean isDistinct;
    private List<BasicColumn> selectList;
    private SqlTable sqlTable;
    private JoinModel joinModel;
    private TableAliasCalculator tableAliasCalculator;
    private WhereModel whereModel;
    private GroupByModel groupByModel;

    private QueryExpressionModel(Builder builder) {
        this.connector = builder.connector;
        this.isDistinct = builder.isDistinct;
        this.selectList = Objects.requireNonNull(builder.selectList);
        this.sqlTable = Objects.requireNonNull(builder.sqlTable);
        this.joinModel = builder.joinModel;
        this.tableAliasCalculator = getJoinModel().map(jm ->
            GuaranteedTableAliasCalculator.of(builder.tableAliases))
                .orElse(TableAliasCalculator.of(builder.tableAliases));

        this.whereModel = builder.whereModel;
        this.groupByModel = builder.groupByModel;
    }

    public Optional<String> getConnector() {
        return Optional.ofNullable(connector);
    }

    public boolean isDistinct() {
        return isDistinct;
    }

    public <R> Stream<R> mapColumns(Function<BasicColumn, R> mapper) {
        return selectList.stream().map(mapper);
    }

    public SqlTable getSqlTable() {
        return sqlTable;
    }

    public TableAliasCalculator getTableAliasCalculator() {
        return tableAliasCalculator;
    }

    public Optional<WhereModel> getWhereModel() {
        return Optional.ofNullable(whereModel);
    }

    public Optional<JoinModel> getJoinModel() {
        return Optional.ofNullable(joinModel);
    }

    public Optional<GroupByModel> getGroupByModel() {
        return Optional.ofNullable(groupByModel);
    }

    public String calculateTableNameIncludingAlias(SqlTable sqlTable) {
        return sqlTable.name()
                + spaceBefore(tableAliasCalculator.alias(sqlTable));
    }

    public static Builder withSelectList(List<BasicColumn> columnList) {
        return new Builder().withSelectList(columnList);
    }

    public static class Builder {
        private String connector;
        private boolean isDistinct;
        private List<BasicColumn> selectList = new ArrayList<>();
        private SqlTable sqlTable;
        private Map<SqlTable, String> tableAliases = new HashMap<>();
        private JoinModel joinModel;
        private WhereModel whereModel;
        private GroupByModel groupByModel;

        public Builder withConnector(String connector) {
            this.connector = connector;
            return this;
        }

        public Builder withTable(SqlTable table) {
            this.sqlTable = table;
            return this;
        }

        public Builder isDistinct(boolean isDistinct) {
            this.isDistinct = isDistinct;
            return this;
        }

        public Builder withSelectList(List<BasicColumn> selectList) {
            this.selectList = selectList;
            return this;
        }

        public Builder withTableAliases(Map<SqlTable, String> tableAliases) {
            this.tableAliases.putAll(tableAliases);
            return this;
        }

        public Builder withWhereModel(WhereModel whereModel) {
            this.whereModel = whereModel;
            return this;
        }

        public Builder withJoinModel(JoinModel joinModel) {
            this.joinModel = joinModel;
            return this;
        }

        public Builder withGroupByModel(GroupByModel groupByModel) {
            this.groupByModel = groupByModel;
            return this;
        }

        public QueryExpressionModel build() {
            return new QueryExpressionModel(this);
        }
    }
}
